package com.cskaoyan.market.controller.admin;

import com.cskaoyan.market.db.domain.MarketKeyword;
import com.cskaoyan.market.service.MarketKeywordService;
import com.cskaoyan.market.service.impl.MarketKeywordServiceImpl;
import com.cskaoyan.market.util.HttpUtil;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("admin/keyword")
public class KeywordController {
    @Autowired
    private MarketKeywordService marketKeywordService;
    @GetMapping("list")
    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String keyword = req.getParameter("keyword");
        String url = req.getParameter("url");
        List<MarketKeyword> marketKeywords = marketKeywordService.readKeywordList(page, limit, sort, order,keyword,url);
        Object ok = ResponseUtil.okList(marketKeywords);
        //相应体
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String s = objectMapper.writeValueAsString(ok);
        resp.getWriter().println(s);
    }

    @PostMapping("create")
    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //json解析
        String jsonStr = HttpUtil.getRequestBody(req);

        String keyword = JacksonUtil.parseString(jsonStr, "keyword");
        String url = JacksonUtil.parseString(jsonStr, "url");
        String isHot = JacksonUtil.parseString(jsonStr, "isHot");
        String isDefault = JacksonUtil.parseString(jsonStr, "isDefault");
        //业务
        List<MarketKeyword> marketKeywords = marketKeywordService.InsertKeyword(keyword, url,isHot,isDefault);
        if (marketKeywords.size()>0){
            MarketKeyword marketKeyword = marketKeywords.get(0);
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("id",marketKeyword.getId());
            dataMap.put("keyword",marketKeyword.getKeyword());
            dataMap.put("url",marketKeyword.getUrl());
            dataMap.put("isHot",marketKeyword.getIsHot());
            dataMap.put("isDefault",marketKeyword.getIsDefault());
            dataMap.put("addTime",marketKeyword.getAddTime());
            dataMap.put("updateTime",marketKeyword.getUpdateTime());
            Object ok = ResponseUtil.ok(dataMap);
            ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
            String s = objectMapper.writeValueAsString(ok);
            resp.getWriter().println(s);
        }else{
            Object fail = ResponseUtil.fail();
            ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
            String s = objectMapper.writeValueAsString(fail);
            resp.getWriter().println(s);
        }

    }
    @PostMapping("update")
    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //json解析
        String jsonStr = HttpUtil.getRequestBody(req);
        String id = JacksonUtil.parseString(jsonStr, "id");
        String keyword = JacksonUtil.parseString(jsonStr, "keyword");
        String url = JacksonUtil.parseString(jsonStr, "url");
        String isHot = JacksonUtil.parseString(jsonStr, "isHot");
        String isDefault = JacksonUtil.parseString(jsonStr, "isDefault");
        String updateTime = JacksonUtil.parseString(jsonStr, "updateTime");
        String addTime = JacksonUtil.parseString(jsonStr, "addTime");
        List<MarketKeyword> marketKeywords = marketKeywordService.updateKeyword(id, keyword, url,isHot,isDefault,addTime,updateTime);
        if (marketKeywords.size()>0){
            Object ok = ResponseUtil.ok();
            ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
            String s = objectMapper.writeValueAsString(ok);
            resp.getWriter().println(s);
        }else{
            Object fail = ResponseUtil.fail();
            ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
            String s = objectMapper.writeValueAsString(fail);
            resp.getWriter().println(s);
        }
    }
    @PostMapping("delete")
    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //json解析
        String jsonStr = HttpUtil.getRequestBody(req);
        String id = JacksonUtil.parseString(jsonStr, "id");
        String keyword = JacksonUtil.parseString(jsonStr, "keyword");
        String url = JacksonUtil.parseString(jsonStr, "url");
        String isHot = JacksonUtil.parseString(jsonStr, "isHot");
        String isDefault = JacksonUtil.parseString(jsonStr, "isDefault");
        String updateTime = JacksonUtil.parseString(jsonStr, "updateTime");
        String addTime = JacksonUtil.parseString(jsonStr, "addTime");
        List<MarketKeyword> marketKeywords = marketKeywordService.deleteKeyword(id, keyword, url,isHot,isDefault,addTime,updateTime);
        if (marketKeywords.size()>0){
            Object ok = ResponseUtil.ok();
            ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
            String s = objectMapper.writeValueAsString(ok);
            resp.getWriter().println(s);
        }else{
            Object fail = ResponseUtil.fail();
            ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
            String s = objectMapper.writeValueAsString(fail);
            resp.getWriter().println(s);
        }
    }

}
